package com.example.springboot.controller;

import com.alibaba.da.coin.ide.spi.meta.AskedInfoMsg;
import com.alibaba.da.coin.ide.spi.meta.ExecuteCode;
import com.alibaba.da.coin.ide.spi.meta.ResultType;
import com.alibaba.da.coin.ide.spi.meta.SlotEntity;
import com.alibaba.da.coin.ide.spi.standard.ResultModel;
import com.alibaba.da.coin.ide.spi.standard.TaskQuery;
import com.alibaba.da.coin.ide.spi.standard.TaskResult;
import com.alibaba.da.coin.ide.spi.trans.MetaFormat;
import com.example.springboot.service.IDeviceStateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/genie")
@Slf4j
@Api(value = "", tags = "天猫精灵对接")
@AllArgsConstructor
public class GenieController {
    private final IDeviceStateService iDeviceStateService;

    @PostMapping("/getGenieMsg")
    @ApiOperation(value = "数据上报", notes = "天猫精")
    public ResultModel<TaskResult> bindingBuilding(@RequestBody String taskQuery) {
        log.info("天猫精灵测试:{}",taskQuery);
        //返回数据
        TaskQuery query = MetaFormat.parseToQuery(taskQuery);
       // Map<String, String> paramMap = query.getRequestData();
        Map<String, String> paramMap = query.getSlotEntities().stream().collect(Collectors.toMap(slotItem -> slotItem.getIntentParameterName(), slotItem -> slotItem.getOriginalValue()));
         log.info("接收到的数据为:{}",paramMap.toString());

        TaskResult result = new TaskResult();
        if("welcome".equals(query.getIntentName())){
            result.setReply("欢迎使用天气小蜜，使用小蜜可以查询天气哟");
        }else if("weather".equals(query.getIntentName())){
            if(paramMap.get("city")== null){
                result.setReply("您要查询哪个设备的数据?");
                return askReply(result, "city", query.getIntentId());
            }
        }else if ("find_device_info".equals(query.getIntentName())){
            if(paramMap.isEmpty()){
                result.setReply("您要查询哪个设备的数据?");
                return askReply(result,  "device_no", query.getIntentId());
            }else {
                if(paramMap.get("device_no")== null){
                    result.setReply("您要查询哪个设备的数据?");
                    return askReply(result,  "device_no", query.getIntentId());
                }
            }

            result.setReply(iDeviceStateService.selectDeviceState(paramMap.get("device_no"),paramMap.get("parameterType")));
        }else if("device_info_type".equals(query.getIntentName())){
          if(paramMap.get("device_no")== null){
                result.setReply("您要查询哪个设备的数据?");
                return askReply(result,  "device_no", query.getIntentId());
            }
            result.setReply(iDeviceStateService.selectDeviceState(paramMap.get("device_no"),paramMap.get("parameterType")));
        }else {
            result.setReply("请检查意图名称是否正确，或者新增的意图没有在代码里添加对应的处理分支。");
        }

        return reply(result);
    }
    @PostMapping("/aligenie//home/wangl/Downloads/26269d202b467e8f2e743b060a9fe685.txt")
    public String authentication(){
        return "Jfc4Z4Ur15JwUBuvUQD5wg7Nu8+l+HscqYlfofbyJda0SZFBn4Rz365YATsKQvI2";//证书内容
    }
    /**
     * 结束对话的回复，回复后音箱闭麦
     * @param taskResult
     * @return
     */
    private ResultModel<TaskResult> reply(TaskResult taskResult){
        ResultModel<TaskResult> res = new ResultModel<>();
        taskResult.setExecuteCode(ExecuteCode.SUCCESS);
        taskResult.setResultType(ResultType.RESULT);
        res.setReturnCode("0");
        res.setReturnValue(taskResult);
        return res;
    }
    /**
     * 指定追问参数，音箱自动开麦，用户的回答优先匹配追问的参数
     * @param taskResult
     * @param parameterName
     * @param intentId
     * @return
     */
    private ResultModel<TaskResult> askReply(TaskResult taskResult, String parameterName, Long intentId){
        ResultModel<TaskResult>res=new ResultModel<>();
        taskResult.setExecuteCode(ExecuteCode.SUCCESS);
        taskResult.setResultType(ResultType.ASK_INF);
        AskedInfoMsg askedInfoMsg= new AskedInfoMsg();
        askedInfoMsg.setIntentId(intentId);
        askedInfoMsg.setParameterName(parameterName);
        List<AskedInfoMsg> askedInfos = new ArrayList<>();
        askedInfos.add(askedInfoMsg);
        taskResult.setAskedInfos(askedInfos);
        res.setReturnValue(taskResult);
        return res;
    }

}
